public class L700 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(4);
        TreeNode treeNode1 = new TreeNode(7);
        treeNode.right = treeNode1;
        TreeNode treeNode2 = new Solution().searchBST(treeNode, 5);
        System.out.println(treeNode2.toString());
    }


    static class Solution {
        public TreeNode searchBST(TreeNode root, int val) {
            if (root == null) {
                return null;
            }
            if (root.val > val) {
                TreeNode treeNode = searchBST(root.left, val);
                if (treeNode != null) {
                    return treeNode;
                }
            } else if (root.val < val) {
                TreeNode treeNode = searchBST(root.right, val);
                if (treeNode != null) {
                    return treeNode;
                }
            } else {
                return root;
            }
            return null;
        }
    }
}
